package me.helllp.book.concurrent.ch03;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * 信号量的使用
 * 
 * @author Administrator
 *
 */
public class SemapDemo implements Runnable{
	final Semaphore semp = new Semaphore(5);
	
	@Override
	public void run() {
		try {
			semp.acquire();
			//	进行模拟业务逻辑操作
			Thread.sleep(2000);
			System.out.println(Thread.currentThread().getId() + " : done!");
			semp.release();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		final SemapDemo obj = new SemapDemo();
		
		ExecutorService exec = Executors.newFixedThreadPool(20);
		
		for(int i=0; i<20; i++){
			exec.submit(obj);
		}
	}
}
